import 'package:flutter/material.dart';

import 'weather_detail.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Weather',
      theme: ThemeData(
        primarySwatch: MaterialColor(
          Colors.white.value,
          <int, Color>{
            50: Colors.white,
            100: Colors.white,
            200: Colors.white,
            300: Colors.white,
            400: Colors.white,
            500: Colors.white,
            600: Colors.white,
            700: Colors.white,
            800: Colors.white,
            900: Colors.white,
          },
        ),
        backgroundColor: Colors.white
      ),
      home: MyHomePage(title: 'Flutter Weather'),
      debugShowCheckedModeBanner: false,
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  TabController _tabController;

  var tabTitles = ["Today", "Tomorrow", "Next Week"];

  @override
  void initState() {
    super.initState();
    _tabController = TabController(vsync: this, length: tabTitles.length)
      ..addListener(() {
        print("tab selected index = ${_tabController.index}");
        setState(() {});
      });
  }
  GlobalKey _myKey = new GlobalKey();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        key: _myKey,
        title: Text(widget.title),
        centerTitle: true,
        leading: IconButton(
          icon: const Icon(Icons.menu),
          onPressed: () {},),
        actions: <Widget>[
          IconButton(
            icon: const Icon(Icons.search),
            onPressed: () {},
          )
        ],
        backgroundColor: Colors.white,
        elevation: 0.0,
      ),
      body: Center(
        child: Stack(
          alignment: Alignment.center,
          children: <Widget>[
            Column(children: <Widget>[
              _buildTabBar(),
              SizedBox(height: 20.0),
              _buildTabBarView(),
            ]),
            Positioned(
              bottom: 30.0,
              child: _buildCursor(),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildTabBarView() {
    var height = MediaQuery.of(context).size.height - (20.0 + 20.0 + 28.0 + 50.0 + 20.0 + 48.0 + 20.0);
    return  DetailWidget("",height);
//    return TabBarView(
//      controller: _tabController,
//      physics: BouncingScrollPhysics(),
//      children: <Widget>[
//        DetailWidget("",height),
//        DetailWidget("",height),
//        DetailWidget("",height),
//      ],
//    );
  }

  Widget _buildTabBar() {
    return Container(
      color: Colors.transparent,
      height: 38.0,
      margin: const EdgeInsets.only(top: 20.0, left: 30.0, right: 30.0),
      child: TabBar(
          isScrollable: false,
          controller: _tabController,
          labelColor: Color(0xFF5D50FE),
          unselectedLabelColor: Color(0xFF130E51),
          labelStyle: TextStyle(fontSize: 16.0),
          indicator: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(19.0)),
            color: Color(0xFFADA7FE),
          ),
          tabs: tabTitles.map((title) {
            return Tab(
              text: title,
            );
          }).toList()),
    );

  }
  Widget _buildCursor(){
    return Center(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: tabTitles.map((str){
          if(tabTitles[_tabController.index] == str) {
            return Padding(padding: EdgeInsets.all(5.0),child: CircleAvatar(backgroundColor: Color(0xFF0051FF),radius: 5.0,),);
          }
          return Padding(padding: EdgeInsets.all(5.0),child: CircleAvatar(backgroundColor: Color(0xFFBBC0D4),radius: 5.0,),);
        }).toList(),
      ),
    );
  }

}
